package app

import (
	"fmt"
	"net/url"
	"uni-core/globals"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
)

// createProjectInfoTab 创建项目信息标签页
func createProjectInfoTab(w fyne.Window, a fyne.App) *fyne.Container {
	// 获取应用程序名称
	appName := a.Metadata().Name
	if appName == "" {
		appName = globals.DefProjectName
	}

	// 创建项目名称卡片
	nameCard := container.NewVBox(
		widget.NewLabelWithStyle("项目名称", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
		widget.NewSeparator(),
		widget.NewLabelWithStyle(appName, fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
	)
	nameCard = container.NewPadded(nameCard)

	// 创建版本信息卡片
	versionCard := container.NewVBox(
		widget.NewLabelWithStyle("版本信息", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
		widget.NewSeparator(),
		widget.NewLabelWithStyle(fmt.Sprintf("当前版本: %s", a.Metadata().Version), fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
	)
	versionCard = container.NewPadded(versionCard)

	// 创建项目链接按钮
	urlButton := widget.NewButtonWithIcon("访问项目", theme.HelpIcon(), func() {
		parsedURL, err := url.Parse(globals.ProjectURL)
		if err != nil {
			dialog.ShowError(err, w)
			return
		}
		if err := a.OpenURL(parsedURL); err != nil {
			dialog.ShowError(err, w)
			return
		}
	})
	urlButton.Importance = widget.HighImportance

	// 创建卡片式布局
	content := container.NewVBox(
		layout.NewSpacer(),
		container.NewHBox(
			layout.NewSpacer(),
			nameCard,
			layout.NewSpacer(),
			versionCard,
			layout.NewSpacer(),
		),
		layout.NewSpacer(),
		container.NewCenter(urlButton),
		layout.NewSpacer(),
	)

	return container.NewPadded(content)
}
